<?php

namespace App\Http\Controllers\Api;

use App\Libs\WeChat\WxSmallClient;
use App\Models\goods;
use App\Models\like_count;
use App\Models\order_info;
use App\Models\user;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Session;

class CartController extends Controller
{
    /**
     * @param Request $request
     * 添加购物车
     */
    public function addCart(Request $request){
        $info = $request->json('goods_array');//['goods_id'=>1,'goods_num'=>2]
        if (!$info) fun_respon(0, '系统繁忙，请稍后再试');
        $openid = $request->openid;
        $keys = $openid.'cart';
        //判断购物车是否有商品 如果有 先清除 然后更新 如果没有直接添加
        $goods = Redis::get($keys);
        if($goods){
            $goods_array = json_decode($goods,true);
            $isHave = false;
            foreach ($goods_array as &$value){
                    if($value['goods_id']==$info['goods_id']){
                        $isHave = true;
                        $value['num'] += $info['num'];
                    }
                    //$isHave = true;
                    //foreach ($value['goods_info'] as $key=>&$v) {

                        //if($v['sku_id'] == $info['goods_info'][$key]['sku_id']){

                            //$v['number'] += 1;
                            //$value['goods_info'][$key] = $v;
                        //}
                    //}
                continue;
            }
            if(!$isHave){
                $goods_array[] = $info;
            }
            $good = $goods_array;
        }else{
            $good[] = $info;
        }

        $cart_list = json_encode($good);
        Redis::set($keys,$cart_list);
        fun_respon(1, '添加成功！');

    }


    /**
     * @param Request $request
     * 更新
     */
    public function update(Request $request){
        $goods_array = $request->json('goods_array');
        $openid = $request->openid;
        $key = $openid.'cart';
        $info = json_encode($goods_array);
        $num = 0;
        foreach ($goods_array as $value){
            if(!$value['goods_id']) fun_respon(0, '商品信息错误！');
            $goods_detail = goods::where(['id'=>$value['goods_id']])->first(['id','title','retail_price','discount_price','original_price','goods_cover','goods_type','description']);
            if(!$goods_detail){
                fun_respon(0, '系统繁忙，请稍后再试');
            }
            if($value['num'] <= 0){
                fun_respon(0, '数量不能为0！');
            }
            /*//计算价格和数量
            foreach ($value['goods_info'] as &$v) {
                $num += $v['number'];
            }*/
            $num += $value['num'];
        }
        if($num==0 ){
            $info = '';
        }
        Redis::set($key,$info);
        fun_respon(1, '添加成功！');
    }

    /**
     * @param Request $request
     * 清空购物车
     */
    public function del(Request $request){
        $openid = $request->openid;
        $key = $openid.'cart';
        if(Redis::exists($key)){
            Redis::set($key,'');
        }
        fun_respon(1, '操作成功！');
    }

    /**
     * @param Request $request
     * 购物车列表
     */
    public function getCart(Request $request){
        $openid = $request->openid;
        $key = $openid.'cart';
        //判断购物车中是否存在商品
        if(Redis::get($key)){
            $goods_array = json_decode(Redis::get($key),true);
            $total = 0;
            $isHave = false;
            foreach ($goods_array as $keys =>&$value){
                if(!$value['goods_id']) fun_respon(0, '商品信息错误！');
                $goods_detail = goods::where(['id'=>$value['goods_id']])->first(['id','title','discount_price','retail_price','original_price','goods_cover','goods_type','description']);
                if(!$goods_detail){
                    fun_respon(0, '系统繁忙，请稍后再试');
                }
                $value['title'] = $goods_detail['title'];
                $value['retail_price'] = $goods_detail['retail_price'];
                $value['original_price'] = $goods_detail['original_price'];
                $value['discount_price'] = $goods_detail['discount_price'];
                $value['goods_cover'] = $goods_detail['goods_cover'];
                $value['description'] = $goods_detail['description'];
                //$price = $goods_detail['retail_price'];
                //计算价格和数量
                /*foreach ($value['goods_info'] as $item=>&$v) {
                    $v['allPrice'] = $v['number']*19.8;
                    if($status==0 && $v['number'] > 0 && (!$isHave)){
                        $v['allPrice'] -= 9.9;
                        $isHave = true;
                    }
                    $total += $v['allPrice'];
                }*/

            }

            //$data['is_buy'] = $status;
            $data['total'] = $total;
            $data['freight'] = get_freight();
            $data['goods_array'] = $goods_array ;
            fun_respon(1, $data);
        }else{
            fun_respon(0, '暂无商品！');
        }

    }
}